﻿using AutoStar.ViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace AutoStar.Views
{
    /// <summary>
    /// Serial.xaml 的交互逻辑
    /// </summary>
    public partial class WindowSerial : Window
    {
        private static WindowSerial instance;
        public WindowSerial()
        {
            InitializeComponent();
            //VM = new WindowSerialVM();
            //DataContext = VM;
            DataContext = Global.serialVM;
            this.Loaded += WindowsSerial_Loaded;
            this.Closing += WindowsSerial_Closing;
            //Global.serialVM.SendListBox.CollectionChanged += SendListBox_CollectionChanged;
            //Global.serialVM.ReceiveListBox.CollectionChanged += ReceiveListBox_CollectionChanged;
        }

        //private void ReceiveListBox_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        //{
        //    if (e.Action == NotifyCollectionChangedAction.Add)
        //    {
        //        ReceiveListBoxName.ScrollIntoView(e.NewItems[0]);
        //        //foreach (var item in e.NewItems)
        //        //{
        //        //    ReceiveListBoxName.ScrollIntoView(item);
        //        //}
        //    }
        //}

        //private void SendListBox_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        //{
            //if (e.Action == NotifyCollectionChangedAction.Add)
            //{
            //    //foreach (var item in e.NewItems)
            //    //{
            //    //    SendListBoxName.ScrollIntoView(item);
            //    //}
            //    SendListBoxName.ScrollIntoView(e.NewItems[0]);
            //}
        //}

        private void WindowsSerial_Closing(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
            //Global.serialVM.WindowsSerial_Closing();
            //VM.WindowsSerial_Closing();
        }

        public static WindowSerial GetInstance()
        {
            if (instance == null)
            {
                instance = new WindowSerial();
            }
            return instance;
        }

        private void WindowsSerial_Loaded(object sender, RoutedEventArgs e)
        {
            Global.serialVM.Load();
            //VM.Load();
        }

        private void ComboBox_DropDownOpened(object sender, EventArgs e)
        {
            Global.serialVM.SerialPorts = new ObservableCollection<string>();
            string[] ports = System.IO.Ports.SerialPort.GetPortNames();
            foreach (string port in ports)
            {
                Global.serialVM.SerialPorts.Add(port);
            }
            if (Global.serialVM.SerialPorts.Count > 0)
                Global.serialVM.SelectedSerialPorts = Global.serialVM.SerialPorts[0];
            else
                Global.serialVM.SelectedSerialPorts = "";
        }
    }
}
